package memorphic.slide.debugger
{
	import flash.display.DisplayObjectContainer;
	import flash.events.AsyncErrorEvent;
	import flash.events.StatusEvent;
	import flash.events.TimerEvent;
	import flash.net.LocalConnection;
	import flash.utils.Timer;
	
	import memorphic.logging.LogType;
	
	import mx.logging.Log;
	import mx.utils.UIDUtil;
	
	public class SlideDebugger2
	{
		
		private var debuggerConnection:LocalConnection;
		private static var LC_RETRY_TIMEOUT:int = 1000;
		private var lcRetryTimer:Timer;
		
		private var viewHighlighter:ViewHighlighter;
		
		private static var instance:SlideDebugger2;
		
		private var rootView:DisplayObjectContainer;
		
		public var overlayOptions:OverlayOptions = new OverlayOptions();
		
		public var appUID:String;
		public var appName:String;
		
		public function SlideDebugger2()
		{
		}

		private function construct():void
		{
			appUID = appName = "_app" + UIDUtil.createUID();
			viewHighlighter = new ViewHighlighter();
			viewHighlighter.overlayOptions = overlayOptions;
			debuggerConnection = new LocalConnection();
			debuggerConnection.allowDomain("*");
			debuggerConnection.client = this;
			debuggerConnection.connect(appUID);
			debuggerConnection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onLCAsyncError);
			debuggerConnection.addEventListener(StatusEvent.STATUS, onLCStatus);
			registerApp();
			
		}
		
		
		public function registerAppResponse():void
		{
			lcRetryTimer.stop();
		}
		
		public function pingResponse():void
		{
			
		}
		
		private function registerApp():void
		{
			if(!lcRetryTimer){
				lcRetryTimer = new Timer(LC_RETRY_TIMEOUT, 1);
				lcRetryTimer.addEventListener(TimerEvent.TIMER, onLcRetryTimer);
			}
			lcRetryTimer.reset();
			lcRetryTimer.start();
			debuggerConnection.send("_slide_debugger", "registerApp", appUID);
		}
		
		
		private function onLcRetryTimer(e:TimerEvent):void
		{
			trace("retry");
			registerApp();
		}
		
		private function onLCAsyncError(e:AsyncErrorEvent):void
		{
			trace(e.text);
			Log.getLogger(LogType.NETWORK).info(e.text);
		}
		private function onLCStatus(e:StatusEvent):void
		{
			trace(e.code);
			Log.getLogger(LogType.NETWORK).info(e.code);
		}
		
		private function getFileName(location:String):String
		{
			var lastSlash:int = location.lastIndexOf("/");
			if(lastSlash == -1){
				return location;
			}else{
				return location.substr(lastSlash+1);
			}
		}
		
		
		public static function init(rootView:DisplayObjectContainer):void
		{
			if(!instance){
				instance = new SlideDebugger2();
				instance.rootView = rootView;
				instance.construct();
			}
		}
		

		
		
		public function updateOverlay(options:OverlayOptions=null):void
		{
			if(options)
			{
				overlayOptions = options;
			}
			viewHighlighter.overlayOptions = overlayOptions;
			viewHighlighter.highlightViews(rootView);
		}
		
	}
}
	import memorphic.slide.debugger.SlideDebugger2;
	import flash.net.LocalConnection;
	

class LCClient 
{
	
	private var host:SlideDebugger2;
	
	private var responderLC:LocalConnection = new LocalConnection();
	
	function LCClient(host:SlideDebugger2)
	{
		this.host = host;
	}
	
	public function ping(fromLC:String):void
	{
		responderLC.send(fromLC, "pingResponse", host.appUID);
	}
	
}
